package com.pi.uc.service;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.pi.base.dto.result.respcode.user.UcResp;
import com.pi.base.enumerate.record.RecordState;
import com.pi.base.exception.ServiceException;
import com.pi.base.util.bean.ObjectTool;
import com.pi.uc.dao.entity.UcUserFollowEntity;
import com.pi.uc.dao.entity.UcUserShareEntity;
import com.pi.uc.dao.mapper.UcUserFollowMapper;
import com.pi.uc.dao.param.UcUserFollowParam;
import com.pi.uc.enumerate.FollowStatus;
import com.pi.uc.facade.UserFollowFacade;
import com.pi.uc.vo.follow.UserFollowSketchVO;
import com.pi.uc.vo.follow.UserFollowVo;

@Validated
@Service
public class UcUserFollowService {
  private static final Logger logger = LoggerFactory.getLogger(UcUserFollowService.class);
  @Autowired
  private UcUserShareService userShareService;
  @Autowired
  private UserFollowFacade userFollowFacade;
  @Autowired
  private UcUserFollowMapper userFollowMapper;
  
  public List<UserFollowVo> queryUserFollowList(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId){
    List<UcUserShareEntity> userShareList = 
        userShareService.queryUserShare(userId);
    List<Long> toBeFollowed = 
        userShareList.stream().map(UcUserShareEntity::getUserId).collect(Collectors.toList());
    List<UcUserFollowEntity> followList = queryUserFollow(userId);
    return userFollowFacade.transLateUserFollow(followList, toBeFollowed);
  }
  
  public UserFollowSketchVO queryUserFollowSketch(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId){
    List<UcUserShareEntity> userShareList = 
        userShareService.queryUserShare(userId);
    List<Long> toBeFollowed = 
        userShareList.stream().map(UcUserShareEntity::getUserId).collect(Collectors.toList());
    List<UcUserFollowEntity> followList = queryUserFollow(userId);
    return userFollowFacade.transLateUserFollowSketch(followList, toBeFollowed);
  }
  
  public List<UcUserFollowEntity> queryUserFollow(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId){
    //查询用户关注的人员列表
    UcUserFollowParam param = new UcUserFollowParam();
    param.setUserId(userId);
    return userFollowMapper.findList(param);
  }
  public UcUserFollowEntity queryUserFollow(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId,
      @NotNull(message = "UC_USER.FOLLOW_USER_NOT_FOUND") Long followUser){
    //查询用户关注的人员列表
    UcUserFollowParam param = new UcUserFollowParam();
    param.setUserId(userId);
    param.setFollowUser(followUser);
    return ObjectTool.getOne(userFollowMapper.findList(param));
  }
  
  public void follow(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId,
      @NotNull(message = "UC_USER.FOLLOW_USER_NOT_FOUND") Long followUser){
    smartfollow(userId, followUser, FollowStatus.FOLLOW);
  }
  public void unFollow(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId,
      @NotNull(message = "UC_USER.FOLLOW_USER_NOT_FOUND") Long followUser){
    smartfollow(userId, followUser, FollowStatus.UNFOLLOW);
  }
  public void smartfollow( 
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId, 
      @NotNull(message = "UC_USER.FOLLOW_USER_NOT_FOUND") Long followUser, 
      @NotNull(message = "UC_USER.FOLLOW_STATUS_NOT_FOUND") Boolean follow){
    smartfollow(userId, followUser, FollowStatus.getFollowStatus(follow));
  }
  private void smartfollow( Long userId, Long followUser, FollowStatus followStatus){
    //查询是否有分享过
    UcUserShareEntity share = 
        userShareService.queryUserShare(userId, followUser);
    if(null == share){
      throw new ServiceException(UcResp.RESP_SHARE_NOT_FOUND);
    }
    UcUserFollowEntity follow = queryUserFollow(userId, followUser);
    if(null != follow && FollowStatus.getFollowStatus(follow.getFollowStatus()) == followStatus){
      logger.info("重复操作: {}, 用户:{}, 被执行用户： {}, 暂不执行", followStatus.getStatus(), userId, followUser);
      return; 
    }
    if(null == follow){
      addFellow(userId, followUser, followStatus);
    }else{
      follow.setFollowStatus(followStatus.isFollow());
      userFollowMapper.updateById(follow);
    }
    
  }
  
  private void addFellow(Long userId, Long followUser, FollowStatus followStatus){
    UcUserFollowEntity entity = new UcUserFollowEntity();
    entity.setFollowStatus(followStatus.isFollow());
    entity.setFollowUser(followUser);
    entity.setUserId(userId);
    entity.setState(RecordState.STATE_NORMAL.getCode());
    userFollowMapper.insert(entity);
  }

}
